import {
  getImage,
  getPage
} from '../../utils/config';
import {
  promisify,
  $empty,
  storage,
  $toast,
  http,
  Validate
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
import api from '../../utils/api';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    logged: 0, //判断是否登录 0未登录 1已登录
    logoImg: getImage.logo,
    wechatImg: getImage.wechat,
    pwIcon: 'closed-eye',
    pwType: true,
    smsCode: '',
    smsCodeBtn: false,
    smsCodeText: '获取验证码',
    loginSwitchType: true,
    loginSwitchText: '短信验证登录',
    userId: 0
  },



  /**
   * 获取用户信息
   */
  getUserInfo(e) {

    promisify(wx.getSetting)().then((res) => {
      if (!res.authSetting['scope.userInfo']) {
        return false;
      }

      const {
        userInfo
      } = e.detail;
      console.log(userInfo);
      promisify(wx.login)().then((resCode) => {
        if (resCode.code) {
          console.log(this.data.userId);
          http.post(api.default, {
            data: {
              action: 'WeChat_FX_Login',
              code: resCode.code,
              nickname: userInfo.nickName,
              headurl: userInfo.avatarUrl,
              userid: this.data.userId
            }
          }).then((res) => {
            const {
              data
            } = res.data;
            console.log(res.data);
            if (res.data.code == 0) {
              //登录成功存储用户信息
              app.setLogon(data, 3, () => {
                //登录后跳转
                $toast(`登录成功`, {
                  icon: 'success',
                  times: 1000,
                }).then(() => {

                  wxApi.switchTab(getPage.home);

                })
              });
            } else {
              //登录失败
              $toast(data.errmsg, {
                icon: 'success',
                times: 1000,
              }).then(() => {
                wxApi.navigateBack(1);
              })
            }
          });
        }
      })
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //携带参数
    if (JSON.stringify(options) != '{}') {
      this.setData({
        userId: options.id
      });
    }
    


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})